//
//  BusinessStoreListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/16.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_GDMapManager.h"
#import "TT_GDAnnotationView.h"
#import "BusinessStoreListCell.h"

#import "BusinessStoreListViewController.h"

@interface BusinessStoreListViewController ()<
UITableViewDelegate,
UITableViewDataSource,
MAMapViewDelegate,
AMapSearchDelegate,
AMapLocationManagerDelegate
>

@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, strong) UITableView *tableview;

@end

@implementation BusinessStoreListViewController

- (void)dealloc {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"门店列表";
    
    [self.view addSubview:self.tableview];

    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(240);
    }];
    

    [self.view addSubview:self.mapView];
    
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.tableview.mas_top);
    }];
    
    
    NSMutableArray <MAMultiPointItem *>*items = @[].mutableCopy;
    NSMutableArray *annotations = @[].mutableCopy;
    for (CommonSellerModule *sellerModel in self.dataArray) {
        
        MAMultiPointItem *item = [[MAMultiPointItem alloc] init];
     
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = sellerModel.lat;
        coordinate.longitude = sellerModel.lng;
        item.coordinate = coordinate;
        //
        [items addObject:item];
        
        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
        pointAnnotation.coordinate = coordinate;
        pointAnnotation.title = sellerModel.sellerName;
        pointAnnotation.subtitle = sellerModel.sellerAddress;
        pointAnnotation.lockedToScreen = NO;

        [annotations addObject:pointAnnotation];
    }
    
    MAMultiPointOverlay *overlay = [[MAMultiPointOverlay alloc] initWithMultiPointItems:items];
    [self.mapView addOverlay:overlay];
    [self.mapView addAnnotations:annotations];
    
    [self.tableview reloadData];
    
    if (self.dataArray.count > 0) {
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = self.dataArray.firstObject.lat;
        coordinate.longitude = self.dataArray.firstObject.lng;
        
        self.mapView.centerCoordinate = coordinate;
    }
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BusinessStoreListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BusinessStoreListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    cell.storeName.text = self.dataArray[indexPath.row].sellerName;
    cell.storeAddress.text = self.dataArray[indexPath.row].sellerAddress;
    
    WEAKSELF
    cell.BusinessStoreListCellBlock = ^(BusinessStoreListActionType type) {
      
        switch (type) {
            case BusinessStoreListActionType_call:{
                [[TT_PhonaCallManager manager] callWithStoreId:weakSelf.dataArray[indexPath.row].sellerId];
            } break;
            case BusinessStoreListActionType_address:{
                [weakSelf gotoMapWithIndex:indexPath.row];
            } break;
            case BusinessStoreListActionType_im:{
                [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.dataArray[indexPath.row].sellerId];
            } break;
            default:
                break;
        }
    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = self.dataArray[indexPath.row].lat;
    coordinate.longitude = self.dataArray[indexPath.row].lng;
    
    self.mapView.centerCoordinate = coordinate;
}

#pragma mark - 去地图展示路线
/** 去地图展示路线 */
- (void)gotoMapWithIndex:(NSInteger)index {

    CGFloat latitude  = self.dataArray[index].lat;  // 纬度
    CGFloat longitude = self.dataArray[index].lng; // 经度
    NSString *address = self.dataArray[index].sellerAddress; // 送达地址
    
    [Utilities callMapWithLat:latitude lng:longitude AndAddress:address];
}

#pragma mark 气泡&&大头针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    
//    WEAKSELF
    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        return nil;;
    }
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        TT_GDAnnotationView *annotationView = (TT_GDAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[TT_GDAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:reuseIndetifier];
        }
        
        annotationView.callOutView.hidden = YES;
        annotationView.image = [UIImage imageNamed:@"map_dingwei"];
        annotationView.canShowCallout = NO;
        annotationView.draggable = NO;        //设置标注可以拖动，默认为NO
        
        return annotationView;
    }
    return nil;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"BusinessStoreListCell" bundle:nil] forCellReuseIdentifier:@"BusinessStoreListCellID"];
    }
    return _tableview;
}

- (MAMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MAMapView alloc] init];
        ///如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
        _mapView.showsUserLocation = YES;
        _mapView.userTrackingMode = 1;
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
        //设置地图缩放比例，即显示区域
        [_mapView setZoomLevel:15.1 animated:YES];
        _mapView.delegate = self;
        //设置定位精度
        _mapView.desiredAccuracy = kCLLocationAccuracyBest;
        //设置定位距离
        _mapView.distanceFilter = 5.0f;
    }
    return _mapView;;
}



@end
